// pages/personalInfo/personalInfo.js
import { updateUser } from '../../server/user';
import { verification } from '../../utils/verification'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    personalInfo: wx.getStorageSync('userInfo').name,
    isPersonalInfo: "",
    error_PersonalInfo: ""
  },
  onLoad: function (options) {
    console.log(wx.getStorageSync('userInfo').name);
  },

  updatePersonalInfo() {
    if (verification.JudgmentNull(this.data.personalInfo)) {
      this.setData({
        error_PersonalInfo: "",
        isPersonalInfo: true
      })
    } else {
      this.setData({
        isPersonalInfo: false,
        error_PersonalInfo: "名称不能为空"
      })
    }
  },
  submitPersonalInfo() {
    if(this.data.isPersonalInfo){
      const data = {
        name:this.data.personalInfo
      }
      updateUser(data).then( () => {
        Toast({
          message: "修改成功",
          type: 'success',
          onClose: () => {
            wx.navigateBack({
              delta: 1
            })
          }
        });
      })
    }else{
      this.updatePersonalInfo()
    }
  }

})